import React from 'react';

import Header from '../common/Header';
import CKTable from '../components/CKTable';
import CKModal from '../components/CKModal';
import CKPages from '../components/CKPages';

import moment from 'moment';

import Fetch from '../common/Fetch';

class Tag extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            page: {
                pageSize: 20,
                currentPage: 1,
                totalNum: 43
            },
            data: null,
            currentPage: parseInt(this.props.params.id) || 1,
            dataCount: 1,
            pageNum: 10
        };
    }

    loadCategory(page) {
        this.modal.load('正在获取数据中...');
        Fetch('/cms/article_category_manage/get_tags', {
            num: this.state.pageNum,
            page: page
        }, (res)=> {
            if (res.status) {
                this.setState({
                    data: res.data.data.length > 0 ? res.data.data : null,
                    currentPage: page,
                    dataCount: res.data.count
                });

                this.modal.closeModal();
            } else {
                this.modal.alert('失败', '调用标签列表出错！' + res.msg);
            }
        });
    }

    componentDidMount() {
        this.loadCategory(1);
    }

    render() {
        return (
            <div className="content">
                <Header title="标签"/>
                <div className="content_main">
                    <p>
                        <h4 className="title">列表</h4>
                    </p>
                    <p>
                        {this.state.data ? this.renderTable() : this.renderNotData()}
                    </p>
                </div>
                <CKModal ref={(c)=> {
                    this.modal = c
                }}/>
            </div>
        )
    }

    renderTable() {
        return (
            <div>
                <CKTable ref="table" data={this.state.data} select={false}
                         onRefresh={()=>this.loadCategory(1)}>
                    <CKTable.Header text="ID" field="tag_id"/>
                    <CKTable.Header text="标签名称" field="tag_name"/>
                    <CKTable.Header text="标签数量" field="tag_num"/>
                    <CKTable.Header text="创建时间" field="tag_created_date" onFormat={(val)=> {
                        return moment.unix(val).format('YYYY-MM-DD HH:mm:ss')
                    }}/>
                </CKTable>
                <CKPages current={this.state.currentPage}
                         number={this.state.pageNum}
                         count={this.state.dataCount}
                         onSelect={(page)=> {
                             this.loadCategory(page)
                         }}/>
            </div>
        );
    }

    renderNotData() {
        return (
            <div style={{textAlign: 'center', fontSize: '24px', paddingTop: '200px'}}>
                没有数据
            </div>
        );
    }
}

export default Tag;